// pages/home/home.js
import {
  request,formatNumber
} from '../../utils/util.js'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalName: null, //'course'：课程选择，'store'：分店选择
    page: 1,
    pagesize:10,
    teacherList: [],

    has_more: true,

    day_index: 0,
    time_index:-1,
    today: '',

    store_index: [0, 0, null],
    store_id: null, //筛选店铺id
    store_name: '全部门店', //筛选店铺名称

    course_index: [0, null],
    course_id: null, //筛选课程id
    course_name: '全部课程', //筛选课程名称
    is_change:false
  },
  
  // 获取订阅消息
  getMessInfo: function () {
    request({
      url: 'Login/subscribeTempIds',
      method: 'GET',
      success: res => {
        app.globalData.messInfo = res
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDayList();
    this.getMessInfo();
  },

  onShow: function () {
    this.data.page = 1;
    this.getCourseList();
  },

  //打开弹框
  openModal: function (e) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      modalName: name
    })
  },

  //关闭弹框
  closeModal: function (e) {
    if (e.currentTarget.dataset.is_close == 1) {
      this.setData({
        modalName: null
      })
    }
  },

  //跳转到教师详情页
  toTeacher: function (e) {
    let id = e.currentTarget.dataset.id;
    let course_id = e.currentTarget.dataset.course_id;
    let day_index = this.data.day_index;
    wx.navigateTo({
      url: `/pages/teacher/teacher?id=${id}&day_index=${day_index}&course_id=${course_id}`,
    })
  },

  //获取课程列表
  getCourseList: function () {
    let that = this;
    request({
      url: 'User/userCourse',
      method: 'GET',
      success: data => {
        that.setData({
          courseList: data.user_course_list,
          course_id: data.user_course_list[0].course_id
        })
        that.getTeacherList()
      }
    })
  },

  //获取分店列表
  getStoreList: function () {
    let that = this;
    request({
      url: 'Home/getAddress',
      method: 'GET',
      success: data => {
        that.setData({
          storeList: data
        })
      }
    })
  },

  //弹框店铺选择
  changeStore: function (e) {
    let type = e.currentTarget.dataset.type;
    let index = e.currentTarget.dataset.index;

    if (type == -1) { //全部门店
      this.setData({
        modalName: null,
        store_id: null,
        store_name: '全部门店',
        page: 1,
        teacherList: []
      })
      this.getTeacherList();
      return;
    }

    let store_index = this.data.store_index;
    store_index[type] = index;

    this.setData({
      store_index
    })

    if (type == 2) { //已选完分店
      let {
        storeList,
        store_index,
      } = this.data;
      let store = storeList[store_index[0]].children[store_index[1]].children[store_index[2]];
      this.setData({
        modalName: null,
        store_id: store.id,
        store_name: store.name,
      })
    }
    this.setData({
      page: 1,
      teacherList: []
    })
    this.getTeacherList();
  },

  //弹框课程选择
  changeCourse: function (e) {
    let type = e.currentTarget.dataset.type;
    let index = e.currentTarget.dataset.index;

    if (type == -1) { //全部课程
      this.setData({
        modalName: null,
        course_id: null,
        course_name: '全部课程',
        page: 1,
        teacherList: []
      })
      this.getTeacherList();
      return;
    }

    let course_index = this.data.course_index;
    course_index[type] = index;

    this.setData({
      course_index
    })

    if (type == 1) { //已选完课程
      let {
        courseList,
        course_index,
      } = this.data;
      let course = courseList[course_index[0]].children[course_index[1]];
      this.setData({
        modalName: null,
        course_id: course.course_id,
        course_name: course.course_name,
      })
      this.setData({
        page: 1,
        teacherList: []
      })
      this.getTeacherList();
    }

  },

  //获取n天日期
  getDayList: function () {
    let dayList = [];
    let teacherList = [];
    for (let i = 0; i < 14; i++) {
      let day = this.getDay(i);
      // if (i == 0) {
      //   day.week = '今';
      //   this.setData({
      //     today: day.date
      //   })
      // } else if (i == 1) {
      //   day.week = '明'
      // }
      dayList.push(day)
      teacherList.push([])
    }
    this.setData({
      dayList,
      teacherList,
    })
  },

  //获取日期
  getDay: function (day) {
    let today = new Date();
    let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetday_milliseconds); //注意，这行是关键代码
    let tYear = today.getFullYear();
    let tMonth = today.getMonth();
    let tDate = today.getDate();
    tMonth = (tMonth + 1) < 10 ? ("0" + (tMonth + 1)) : (tMonth + 1);
    tDate = tDate < 10 ? ("0" + tDate) : tDate;
    let time = {
      date: tYear + "-" + tMonth + "-" + tDate,
      day: tDate,
      week: "日一二三四五六".charAt(today.getDay()),
      month: tMonth,
    }
    return time;
  },

  changeDay(e) {
    let {
      index
    } = e.currentTarget.dataset
    this.setData({
      day_index: index,
    })
  },
  changeTime(e){
    let {is_change,time_index}=this.data
    if(!is_change){
      this.setData({is_change:true})
    }
    let {index} = e.currentTarget.dataset
    this.setData({time_index: time_index!=index?index:-1})
    this.getTeacherList();
  },

  //日期改变
  dayChange: function () {
    let {
      dayList,
      day_index
    } = this.data
    this.setData({
      today: dayList[day_index].date,
      page: 1,
    })
    this.getTeacherList();
  },

  //获取可预约教师列表
  getTeacherList: function () {
    let that = this;
    let {
      day_index,
      dayList,
      time_index,
      teacherList,
      page,
      pagesize
    } = this.data;

    let postData = {
      date: dayList[day_index].date,
      hour:time_index!=-1?formatNumber(time_index+8)+':00':'',
      page,
      pagesize
    }
    request({
      url: 'User/courseTeacher',
      method: 'GET',
      data: postData,
      success: data => {
        // console.log(data)
        let {teacher_data,can_subs,count}=data
        let has_more = teacher_data.length == pagesize;
        if (page > 1) {
          page++
          teacherList[day_index] = [...teacherList[day_index], ...teacher_data]
        } else {
          teacherList[day_index] = teacher_data
        }

        that.setData({
          page,
          can_subs,
          count,
          teacherList,
          has_more
        })
      }
    })
  },

  getMore: function () {
    this.data.page++;
    this.getTeacherList();
  },

  swiperChange(e) {
    let index = e.detail.current
    this.setData({
      day_index: index,
    })
    this.dayChange()
  }
})